#pragma once
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include "Tread.hpp"
#include "LockGuard.hpp"
#include "ThreadPool.hpp"
#include "Task.hpp"

int ticks = 100;
int val = 0;

void* hander1(void* args)
{
    pthread_detach(pthread_self());
    std::cout << ++val << std::endl;
}

void* hander2(void* args)
{
    pthread_detach(pthread_self());
    std::cout << ++val << std::endl;
}


pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
void *hander(void* args)
{
    char* name = static_cast<char*>(args);
    while(ticks > 0)
    {  
        if(ticks > 0)
        {
            LockGuard lg(&mtx);
            if(ticks > 0)
            {
                usleep(100);
                printf("get a ticks : %d\n", ticks--);
            }
        }
    } 
}

int main()
{
    ThreadPool<Task> tp;

    while(1)
    {
        int num_l, num_r;
        char op;
        printf("please input num_l : ");
        scanf("%d", &num_l);
        printf("please input num_r : ");
        scanf("%d", &num_r);
        printf("please input op : ");
        // std::cin >> op;
        getchar();
        op = getchar();
        tp.pushTask({num_l, num_r, op});
        tp.start();
    }
    // const int NUM = 20;
    // pthread_t tids[NUM];
    // for(int i = 0; i < NUM; i++)
    // {
    //     char name[128];
    //     snprintf(name, sizeof(name), "thread %d->", i);
    //     pthread_create(tids + i, nullptr, hander, (void*)name);
    // }

    // for(int i = 0; i < NUM; i++)
    // {
    //     pthread_join(tids[i], nullptr);
    // }

    // Thread t1(1, hander1, nullptr);
    // t1.threadRun();

    // pthread_t tid1, tid2;
    // pthread_create(&tid1, nullptr, hander1, nullptr);
    // sleep(1);
    // pthread_create(&tid2, nullptr, hander2, nullptr);
    // pthread_join(tid1, nullptr);
    // pthread_join(tid2, nullptr);
    // sleep(5);
    return 0;
}